home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / StyleSheet.java < prev    next >
Text File  |  1998-06-30  |  8KB  |  295 lines

  1. /*
  2.  * @(#)StyleSheet.java    1.6 98/03/13
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20. package com.sun.java.swing.text.html;
  21.  
  22. import java.util.*;
  23. import java.awt.Font;
  24. import java.awt.FontMetrics;
  25.  
  26. import com.sun.java.swing.text.*;
  27.  
  28. /**
  29.  * Class that holds current style for a document.
  30.  *
  31.  * @author  Jill Nakata
  32.  * @version 1.6 03/13/98
  33.  */
  34. class StyleSheet {
  35.  
  36.     public final static int DEFAULT_FONT_SIZE = 3;
  37.  
  38.     // Member Variables
  39.  
  40.     private int baseFontIndex;
  41.     private int baseFontSize;
  42.     private int    docPtSize;
  43.     private int    docIndex;
  44.     static int sizeMap[];
  45.  
  46.     static {
  47.         // Initialize the sizeMap array from the fonts.sizes property.
  48.         int map[] = new int[32];
  49.     String sizes = "7,8,9,10,12,14,16,18,20,22,24,26,28";
  50. //                0 1 2  3  4  5  6  7  8  9 10 11 12
  51. //docindex                           DI
  52. //current base font index                 BFI
  53. //basefont values                 1  2  3  4  5  6  7
  54.         StringTokenizer s = new StringTokenizer(sizes, ", \t\n");
  55.         int n = 0;
  56.         while ((n < map.length) && s.hasMoreTokens()) {
  57.             map[n++] = Integer.valueOf(s.nextToken()).intValue();
  58.         }
  59.         sizeMap = new int[n];
  60.         System.arraycopy(map, 0, sizeMap, 0, n);
  61.     }
  62.  
  63.     /**
  64.      * StyleSheet constructor originating from a style.
  65.      */
  66.     public StyleSheet(Style s) {
  67.  
  68.       docPtSize = StyleConstants.getFontSize(s);
  69.  
  70.       //
  71.       // baseFontIndex is the index into sizeMap which
  72.       // points to the current base font setting.
  73.       // The default is 3 (DEFAULT_FONT_SIZE) until it is 
  74.       // changed when a <BASEFONT> tag is reached.
  75.       //
  76.       // Here, we're setting base font 3 (DEFAULT) to 
  77.       // be pt size docPtSize.
  78.       //
  79.       baseFontIndex = getIndexOfSize(docPtSize);
  80.       baseFontSize = DEFAULT_FONT_SIZE;
  81.       //
  82.       // docIndex is the index into sizeMap which
  83.       // points to the docPtSize setting.
  84.       // This value changes when the docPtSize changes.
  85.       // Calling this will set docIndex.
  86.       //
  87.       setPtSize(docPtSize);
  88.  
  89.     }
  90.  
  91.     /**
  92.      * StyleSheet constructor originating from default.
  93.      */
  94.     public StyleSheet() {
  95.  
  96.       docPtSize = 12;  // should get this from DEFAULT_STYLE;
  97.  
  98.       //
  99.       // baseFontIndex is the index into sizeMap which
  100.       // points to the current base font setting.
  101.       // The default is 3 (DEFAULT_FONT_SIZE) until it is 
  102.       // changed when a <BASEFONT> tag is reached.
  103.       //
  104.       // Here, we're setting base font 3 (DEFAULT) to 
  105.       // be pt size docPtSize.
  106.       //
  107.       baseFontIndex = getIndexOfSize(docPtSize);
  108.       baseFontSize = DEFAULT_FONT_SIZE;
  109.       //
  110.       // docIndex is the index into sizeMap which
  111.       // points to the docPtSize setting.
  112.       // This value changes when the docPtSize changes.
  113.       // Calling this will set docIndex.
  114.       //
  115.       setPtSize(docPtSize);
  116.  
  117.     }
  118.  
  119.     /**
  120.      * Call with sizes string separated by '.'s.
  121.      * Used in style sheet definitions.
  122.      * e.g., sizes=" 7.8.9.10.12.14.16.18.20.22.24.26.28"
  123.      */
  124.     public void setFontSizes(String sizes) {
  125.         int map[] = new int[32];
  126.         StringTokenizer s = new StringTokenizer(sizes, ". \t\n");
  127.         int n = 0;
  128.         while ((n < map.length) && s.hasMoreTokens()) {
  129.             map[n++] = Integer.valueOf(s.nextToken()).intValue();
  130.         }
  131.         sizeMap = new int[n];
  132.         System.arraycopy(map, 0, sizeMap, 0, n);
  133.  
  134.     //
  135.     // Recalculate index and docindex based on new sizes.
  136.     //
  137.     baseFontIndex = getIndexOfSize(docPtSize);
  138.         //
  139.         // docIndex is the index into sizeMap which
  140.         // points to the docPtSize setting.
  141.         // This value changes when the docPtSize changes.
  142.         // Calling this will set docIndex.
  143.         //
  144.         setPtSize(docPtSize);
  145.  
  146.     }
  147.  
  148.     public void setBaseFontSize(int sz) {
  149.     if (sz < 1)
  150.       baseFontSize = 0;
  151.     else if (sz > 7)
  152.       baseFontSize = 7;
  153.     else
  154.       baseFontSize = sz;
  155.     
  156.     int diff = baseFontSize - DEFAULT_FONT_SIZE;
  157.         baseFontIndex = docIndex + diff;
  158.     if (baseFontIndex > sizeMap.length - 1)
  159.       baseFontIndex = sizeMap.length - 1;
  160.     else if (baseFontIndex < 0)
  161.       baseFontIndex = 0;
  162.     }
  163.  
  164.     public void setBaseFontSize(String size) {
  165.  
  166.       int relSize, absSize, diff;
  167.  
  168.       if (size != null) {
  169.         if (size.startsWith("+")) {
  170.           relSize = Integer.valueOf(size.substring(1)).intValue();
  171.       diff = DEFAULT_FONT_SIZE - (baseFontSize + relSize);
  172.       baseFontIndex = docIndex + diff;
  173.         } else if (size.startsWith("-")) {
  174.           relSize = -Integer.valueOf(size.substring(1)).intValue();
  175.       diff = DEFAULT_FONT_SIZE - (baseFontSize + relSize);
  176.       baseFontIndex = docIndex + diff;
  177.         } else {
  178.           setBaseFontSize(Integer.valueOf(size).intValue());
  179.       diff = baseFontSize - DEFAULT_FONT_SIZE;
  180.           baseFontIndex = docIndex + diff;
  181.         }
  182.       }
  183.  
  184.       if (baseFontIndex > sizeMap.length - 1)
  185.         baseFontIndex = sizeMap.length - 1;
  186.       else if (baseFontIndex < 0)
  187.         baseFontIndex = 0;
  188.  
  189.     }
  190.     
  191.  
  192.     public static int  getIndexOfSize(int pt) {
  193.         for (int i = 0; i < sizeMap.length; i ++ )
  194.                 if (pt <= sizeMap[i])
  195.                         return i;
  196.         return sizeMap.length - 1;
  197.     }
  198.  
  199.  
  200.     /**
  201.      * Return the point size.
  202.      */
  203.     public int getPtSize(int index) {
  204.     if (index < 0)
  205.       return sizeMap[0];
  206.     else if (index > sizeMap.length - 1)
  207.       return sizeMap[sizeMap.length - 1];
  208.     else
  209.       return sizeMap[index];
  210.  
  211.     }
  212.  
  213.     /**
  214.      *  Given a string "+2", "-2", "2".
  215.      *  returns a pt size value
  216.     **/
  217.     public int getPtSize(String size) {
  218.       int relSize, absSize, diff, index;
  219.  
  220.       if (size != null) {
  221.         if (size.startsWith("+")) {
  222.           relSize = Integer.valueOf(size.substring(1)).intValue();
  223.       diff = (baseFontSize + relSize) - DEFAULT_FONT_SIZE;
  224.       index = docIndex + diff;
  225.         } else if (size.startsWith("-")) {
  226.           relSize = -Integer.valueOf(size.substring(1)).intValue();
  227.       diff = (baseFontSize + relSize) - DEFAULT_FONT_SIZE;
  228.       index = docIndex + diff;
  229.         } else {
  230.           absSize = Integer.valueOf(size).intValue();
  231.       diff = absSize - DEFAULT_FONT_SIZE;
  232.           index = docIndex + diff;
  233.         }
  234.     if (index > sizeMap.length - 1)
  235.       index = sizeMap.length - 1;
  236.     else if (index < 0)
  237.       index = 0;
  238.  
  239.     return sizeMap[index];
  240.       }
  241.       return sizeMap[baseFontIndex];
  242.  
  243.  
  244.     }
  245.  
  246.     /**
  247.      * Sets the point size.
  248.     **/
  249.     public void setPtSize(int ptSize) {
  250.       docPtSize = ptSize;
  251.       // Update the docIndex
  252.       docIndex = getIndexOfSize(docPtSize);
  253.       // If docPtSize changed, then reset the
  254.       // baseFontIndex.
  255.       int diff = baseFontSize - DEFAULT_FONT_SIZE;
  256.       baseFontIndex = docIndex + diff;
  257.     }
  258.  
  259.  
  260.     /**
  261.      * Returns the next largest point size.
  262.     **/
  263.     public int getBigger(int ptSize) {
  264.     int index = getIndexOfSize(ptSize);
  265.     if (index + 1 > sizeMap.length - 1)
  266.       index = sizeMap.length - 1;
  267.     else
  268.       index++;
  269.     return sizeMap[index];
  270.     }
  271.  
  272.     /**
  273.      * Returns the next smallest point size.
  274.     **/
  275.     public int getSmaller(int ptSize) {
  276.     int index = getIndexOfSize(ptSize);
  277.     if (index - 1 < 0)
  278.       index = 0;
  279.     else
  280.       index--;
  281.     return sizeMap[index];
  282.     }
  283.  
  284.     /**
  285.      * Given a pt size, return the relative 
  286.      * size value.
  287.     **/
  288.     public int getRelSize(int ptSize) {
  289.     int index = getIndexOfSize(ptSize);
  290.     return index - baseFontIndex;
  291.     }
  292.  
  293. }
  294.  
  295.